package com.app.mingluxing.chat;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.InviteBean;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.Constant;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import de.greenrobot.event.EventBus;

/**
 * 处理好友请求页面, 同意还是拒绝
 * Created by Aaron on 16/1/19.
 */
public class ChatAgreeActivity extends BaseActivity {

    private ImageView ivpic;
    private TextView tvnickname;
    private TextView tvtel;
    private TextView tvcontent;
    private TextView tvrefuse;
    private TextView tvagree;
//    private ChatListBean inviteInfo;
    private InviteBean.InvitedListEntity invitedListEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_chat_agree);

        invitedListEntity = getIntent().getParcelableExtra("inviteInfo");

        initWidget();

        fillData();

    }

    private void fillData() {

        if (invitedListEntity==null||invitedListEntity.user==null)
            return;

        tvcontent.setText(invitedListEntity.inviteWord);
        tvtel.setText(invitedListEntity.user.phoneNum);
        tvnickname.setText(invitedListEntity.user.nickName);

        Glide.with(this)
                .load(invitedListEntity.user.imgUrl)
                .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                .placeholder(R.mipmap.temp_pic).into(ivpic);
    }

    private void initWidget() {

        this.tvagree = (TextView) findViewById(R.id.tv_agree);
        this.tvrefuse = (TextView) findViewById(R.id.tv_refuse);

        this.tvcontent = (TextView) findViewById(R.id.tv_content);
        this.tvtel = (TextView) findViewById(R.id.tv_tel);
        this.tvnickname = (TextView) findViewById(R.id.tv_nickname);
        this.ivpic = (ImageView) findViewById(R.id.iv_pic);

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        tvagree.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        tvrefuse.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.tv_refuse://拒绝
                handleRequest(-1);
                break;

            case R.id.tv_agree://同意
                handleRequest(1);
                break;

            case R.id.iv_left:
                finish();
                break;

        }

    }

    /**
     * 处理好友请求
     */
    private void handleRequest(final int style) {

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("isAgree", style);
        j.put("phoneNum", tvtel.getText().toString().trim());

        HttpUtil.setPostRequest("处理中...", this, Constant.WEB_ROOT_TALK + Constant.DEALINVITE, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                showToast(style == 1 ? "已同意" : "已拒绝");

                                EventBus.getDefault().post(style);

                                finish();

                            } else {
                                showToast(Constant.showErrorMsg(result.getInt("code"), ChatAgreeActivity.this));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);

    }

}
